package com.org.zp.ctrl.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;

public class SearchPositionList extends BaseActivity {
	private ListView listView;
	private PaginationAdapter adapter;
	private TextView title;
	private Button back_btn;
	private Button enable_btn;
	private ProgressDialog progressDialog;
	private String iString = "";
	private RecApp mApp;
	
	private boolean isconnect = true;
	
	private boolean mFullSearch = false;
	
	private List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_class);
		
		if (!(ActivityTaskManager.getInstance().containsName("SearchPositionList"))) {
			ActivityTaskManager.getInstance().putActivity("SearchPositionList",
					this);
		}
		
		mFullSearch = getIntent().getBooleanExtra("full", false);

		mApp = (RecApp) this.getApplication();
		title = (TextView) findViewById(R.id.headTitle);
		back_btn = (Button) findViewById(R.id.back);
		enable_btn = (Button) findViewById(R.id.top_right_button);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity("SearchPositionList");
			}
		});
		enable_btn.setVisibility(View.INVISIBLE);
		
		initializeAdapter();
	}

	/**
	 * 初始化ListView的适配器
	 */
	private void initializeAdapter() {
		
		iString = getIntent().getStringExtra("type");
		if (iString.equals("position_name")) {
			title.setText("选择职位");
			if(mApp.hPositionCategory.size() != 0){
				isconnect = false;
				for(int i = 0; i < mApp.hPositionCategory.size(); i++){
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("code", mApp.hPositionCategory.get(i).get("id"));
					map.put("name", mApp.hPositionCategory.get(i).get("name"));
					list.add(map);
				}
			}
		}
		if (iString.equals("position_hangye")) {
			title.setText("选择行业");
			if(mApp.hPositionIndustry.size() != 0){
				isconnect = false;
				for(int i = 0; i < mApp.hPositionIndustry.size(); i++){
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("code", mApp.hPositionIndustry.get(i).get("id"));
					map.put("name", mApp.hPositionIndustry.get(i).get("name"));
					list.add(map);
				}
			}
		}
		if (iString.equals("position_address")) {
			title.setText("选择地点");
			if(mApp.hPositionCity.size() != 0){
				isconnect = false;
				for(int i = 0; i < mApp.hPositionCity.size(); i++){
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("code", mApp.hPositionCity.get(i).get("id"));
					map.put("name", mApp.hPositionCity.get(i).get("name"));
					list.add(map);
				}
			}
		}
		if (iString.equals("position_positiontype")) {
			title.setText("选择职位性质");
			if(mApp.hPositionNature.size() != 0){
				isconnect = false;
				for(int i = 0; i < mApp.hPositionNature.size(); i++){
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("code", mApp.hPositionNature.get(i).get("id"));
					map.put("name", mApp.hPositionNature.get(i).get("name"));
					list.add(map);
				}
			}
		}
		if (iString.equals("position_companysize")) {
			title.setText("选择公司规模");
			if(mApp.hPositionComSize.size() != 0){
				isconnect = false;
				for(int i = 0; i < mApp.hPositionComSize.size(); i++){
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("code", mApp.hPositionComSize.get(i).get("id"));
					map.put("name", mApp.hPositionComSize.get(i).get("name"));
					list.add(map);
				}
			}
		}
		if (iString.equals("position_salary")) {
			title.setText("选择月薪范围");
			if(mApp.hPositionPay.size() != 0){
				isconnect = false;
				for(int i = 0; i < mApp.hPositionPay.size(); i++){
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("code", mApp.hPositionPay.get(i).get("id"));
					map.put("name", mApp.hPositionPay.get(i).get("name"));
					list.add(map);
				}
			}
		}
		
		initList();
		
		if(isconnect){
			progressDialog = ProgressDialog.show(SearchPositionList.this, "请等待...",  
					"正在加载信息，请稍后...", true, true);
			new QuerySearchSelectAsyncTask().execute(URLHelper.mPrefix, iString);
		}
		
	}
	
	private void initList(){
		adapter = new PaginationAdapter(list);
		listView = (ListView) findViewById(R.id.lvNews);

//		listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(SearchPositionList.this,
						SearchJobActivity.class);
				if (iString.equals("position_name")) {
					title.setText("选择职位");
					
					ActivityTaskManager.getInstance().removeActivity(
							"SearchJobActivity");
					mApp.hPositionId.remove("position_name_second");
					mApp.hPositionId.put("position_name_second",
							list.get(position).get("code"));
					mApp.hPositionName.remove("position_name_second");
					mApp.hPositionName.put("position_name_second", list
							.get(position).get("name"));
					intent.putExtra("full", mFullSearch);
					startActivity(intent);
					ActivityTaskManager.getInstance().removeActivity("SearchPositionList");

				}
				if (iString.equals("position_hangye")) {
					ActivityTaskManager.getInstance().removeActivity(
							"SearchJobActivity");
					title.setText("选择行业");
					mApp.hPositionId.remove("position_hangye");
					mApp.hPositionId.put("position_hangye",
							list.get(position).get("code"));
					mApp.hPositionName.remove("position_hangye");
					mApp.hPositionName.put("position_hangye",
							list.get(position).get("name"));
					intent.putExtra("full", mFullSearch);
					startActivity(intent);
					ActivityTaskManager.getInstance().removeActivity("SearchPositionList");
				}
				if (iString.equals("position_address")) {
					title.setText("选择地点");
					mApp.hPositionId.remove("position_address");
					mApp.hPositionId.put("position_address",
							list.get(position).get("code"));
					mApp.hPositionName.remove("position_address");
					mApp.hPositionName.put("position_address",
							list.get(position).get("name"));
					intent = new Intent(SearchPositionList.this,
							SearchPositionListSecond.class);
					intent.putExtra("full", mFullSearch);
					intent.putExtra("type", "position_address_second");
					intent.putExtra("code", list.get(position).get("code"));
					startActivity(intent);
				}
				if (iString.equals("position_positiontype")) {
					ActivityTaskManager.getInstance().removeActivity(
							"SearchJobActivity");
					title.setText("选择公司性质");
					mApp.hPositionId.remove("position_positiontype");
					mApp.hPositionId.put("position_positiontype",
							list.get(position).get("code"));
					mApp.hPositionName.remove("position_positiontype");
					mApp.hPositionName.put("position_positiontype", list
							.get(position).get("name"));
					intent.putExtra("full", mFullSearch);
					startActivity(intent);
					ActivityTaskManager.getInstance().removeActivity("SearchPositionList");
				}
				if (iString.equals("position_companysize")) {
					ActivityTaskManager.getInstance().removeActivity(
							"SearchJobActivity");
					title.setText("选择公司规模");
					mApp.hPositionId.remove("position_companysize");
					mApp.hPositionId.put("position_companysize",
							list.get(position).get("code"));
					mApp.hPositionName.remove("position_companysize");
					mApp.hPositionName.put("position_companysize", list
							.get(position).get("name"));
					intent.putExtra("full", mFullSearch);
					startActivity(intent);
					ActivityTaskManager.getInstance().removeActivity("SearchPositionList");
				}
				if (iString.equals("position_salary")) {
					ActivityTaskManager.getInstance().removeActivity(
							"SearchJobActivity");
					title.setText("选择月薪范围");
					mApp.hPositionId.remove("position_salary");
					mApp.hPositionId.put("position_salary",
							list.get(position).get("code"));
					mApp.hPositionName.remove("position_salary");
					mApp.hPositionName.put("position_salary",
							list.get(position).get("name"));
					intent.putExtra("full", mFullSearch);
					startActivity(intent);
					ActivityTaskManager.getInstance().removeActivity("SearchPositionList");
				}

			}
		});
	}

	class PaginationAdapter extends BaseAdapter {
		List<HashMap<String, String>> newsItems;

		public PaginationAdapter(List<HashMap<String, String>> newsitems) {
			this.newsItems = newsitems;
		}

		@Override
		public int getCount() {
			return newsItems.size();
		}

		@Override
		public Object getItem(int position) {
			return newsItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if (view == null) {
				if (iString.equals("position_address")) {
					view = getLayoutInflater().inflate(
							R.layout.job_class_items, null);
				} else {
					view = getLayoutInflater().inflate(
							R.layout.search_position_items, null);
				}

			}
			TextView tvTitle = (TextView) view.findViewById(R.id.newstitle);
			tvTitle.setText(newsItems.get(position).get("name"));
			return view;
		}

	}
	
	class QuerySearchSelectAsyncTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			
			if(params[1].equals("position_name")){
				map.put("act", "categorySubclassList");
			}else if(params[1].equals("position_hangye")){
				map.put("act", "categoryList");
				map.put("alias", "QS_trade");
			}else if(params[1].equals("position_address")){
				map.put("act", "districtList");
			}else if(params[1].equals("position_positiontype")){
				map.put("act", "categoryList");
				map.put("alias", "QS_jobs_nature");
			}else if(params[1].equals("position_companysize")){
				map.put("act", "categoryList");
				map.put("alias", "QS_scale");
			}else if(params[1].equals("position_salary")){
				map.put("act", "categoryList");
				map.put("alias", "QS_wage");
			}
			
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Log.e("TMG", "searchPosition result = null");
				progressDialog.dismiss();
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Log.e("TMG", "searchPosition result = timeout");
				progressDialog.dismiss();
			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {
		Log.e("TMG", "searchPosition result = " + str);
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);
				if(iString.equals("position_name")){
					
					String data = json.getString("dataList");
					JSONArray jsonlist = new JSONArray(data);
					for (int i = 0; i < jsonlist.length(); i++) {
						JSONObject log = jsonlist.getJSONObject(i);
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("code", log.getString("id"));
						map.put("name", log.getString("name"));
						list.add(map);
						mApp.hPositionCategory.add(map);
					}
				}else if(iString.equals("position_hangye")){
					
					String data = json.getString("dataList");
					JSONArray jsonlist = new JSONArray(data);
					for (int i = 0; i < jsonlist.length(); i++) {
						JSONObject log = jsonlist.getJSONObject(i);
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("code", log.getString("id"));
						map.put("name", log.getString("name"));
						list.add(map);
						mApp.hPositionIndustry.add(map);
					}
					
				}else if(iString.equals("position_address")){
					
					String data = json.getString("dataList");
					JSONArray jsonlist = new JSONArray(data);
					for (int i = 0; i < jsonlist.length(); i++) {
						JSONObject log = jsonlist.getJSONObject(i);
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("code", log.getString("id"));
						map.put("name", log.getString("name"));
						list.add(map);
						mApp.hPositionCity.add(map);
					}
				}else if(iString.equals("position_positiontype")){
					
					String data = json.getString("dataList");
					JSONArray jsonlist = new JSONArray(data);
					for (int i = 0; i < jsonlist.length(); i++) {
						JSONObject log = jsonlist.getJSONObject(i);
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("code", log.getString("id"));
						map.put("name", log.getString("name"));
						list.add(map);
						mApp.hPositionNature.add(map);
					}
				}else if(iString.equals("position_companysize")){
					
					
					String data = json.getString("dataList");
					JSONArray jsonlist = new JSONArray(data);
					for (int i = 0; i < jsonlist.length(); i++) {
						JSONObject log = jsonlist.getJSONObject(i);
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("code", log.getString("id"));
						map.put("name", log.getString("name"));
						list.add(map);
						mApp.hPositionComSize.add(map);
					}
				}else if(iString.equals("position_salary")){
					
					
					String data = json.getString("dataList");
					JSONArray jsonlist = new JSONArray(data);
					for (int i = 0; i < jsonlist.length(); i++) {
						JSONObject log = jsonlist.getJSONObject(i);
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("code", log.getString("id"));
						map.put("name", log.getString("name"));
						list.add(map);
						mApp.hPositionPay.add(map);
					}
				}
				
				
			} else {
				
			}
			progressDialog.dismiss();
			adapter.notifyDataSetChanged();

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity("SearchPositionList");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
